package com.jonesun.test.javafx;

import javafx.application.Application;
import javafx.application.HostServices;
import javafx.application.Platform;
import javafx.application.Preloader;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class JavafxApplication extends Application {

    private ConfigurableApplicationContext context;
    private final List<Image> defaultIcons = new ArrayList<>();

    @Override
    public void init() throws Exception {
        // 发送进程给预加载器主要通过Application中的notifyPreloader（PreloaderNotification）方法
//        Preloader.ProgressNotification notification=new Preloader.ProgressNotification(0.5);
//        notifyPreloader(notification);
        defaultIcons.addAll(loadDefaultIcons());
        ApplicationContextInitializer<GenericApplicationContext> initializer =
                ac -> {
                    ac.registerBean(Application.class, () -> JavafxApplication.this);
                    ac.registerBean(Parameters.class, this::getParameters);
                    ac.registerBean(HostServices.class, this::getHostServices);
                };
        this.context = new SpringApplicationBuilder()
                .sources(JoneSunFxApplication.class)
                .initializers(initializer)
                .run(getParameters().getRaw().toArray(new String[0]));
    }

    @Override
    public void stop() throws Exception {
        this.context.close();
        Platform.exit();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.getIcons().addAll( defaultIcons);
        this.context.publishEvent(new com.jonesun.test.javafx.StageReadyEvent(primaryStage));
        // 关闭AppPreloader
        notifyPreloader(new Preloader.StateChangeNotification(
                Preloader.StateChangeNotification.Type.BEFORE_START));
    }

    public Collection<Image> loadDefaultIcons() {
        return Arrays.asList(new Image(getClass().getResource("/icons/gear_16x16.png").toExternalForm()),
                new Image(getClass().getResource("/icons/gear_24x24.png").toExternalForm()),
                new Image(getClass().getResource("/icons/gear_36x36.png").toExternalForm()),
                new Image(getClass().getResource("/icons/gear_42x42.png").toExternalForm()),
                new Image(getClass().getResource("/icons/gear_64x64.png").toExternalForm()));
    }

}

class StageReadyEvent extends ApplicationEvent {

    private static final long serialVersionUID = -2735810338882986980L;

    public StageReadyEvent(Stage source) {
        super(source);
    }

    public Stage getStage() {
        return Stage.class.cast(getSource());
    }
}